﻿using System;
using AutoPoco.Engine;

namespace Autopoco.Contrib.DataSources
{
    public class SequentialIterativeLongSource : SequentialLongSource
    {
        private readonly Random _random = new Random(100);
        private int _count;
        private long _current;
        private bool _resetLevels;
        public override long Next(IGenerationSession session)
        {
            if (_count < 1)
            {
                _count = _random.Next(1, 10);
                _current = base.Next(session);
                _resetLevels = true;
            }
            else
                _resetLevels = false;
            _count--;
            return _current;
        }


        public override void Setup(RelationContext context)
        {
            if (!context.Dictionary.ContainsKey(DatasourceConstant.ResetLevels))
                context.Dictionary.Add(DatasourceConstant.ResetLevels, _resetLevels);
            else
                context.Dictionary[DatasourceConstant.ResetLevels] = _resetLevels;
            _resetLevels = false;

        }
    }
}